import './styles/index.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import i18n from '@/i18n'
import { useLanguageStore } from '@/stores/language'
import Vant from 'vant'
import 'vant/lib/index.css'
import App from './App.vue'
import router from './router'
import SvgIcon from './components/SvgIcon/index.vue'

async function loadConfig () {
  try {
    const base = import.meta.env.BASE_URL
    const response = await fetch(`${base}config.json`)
    window.APP_CONFIG = await response.json() // 全局变量存储配置
  } catch (error) {
    console.error('Failed to load config.json:', error)
  }
}

loadConfig().then(() => {
  const app = createApp(App)

  app.use(createPinia())
  app.use(router)
  app.use(Vant)
  app.use(i18n)
  app.component('svg-icon', SvgIcon)

  app.mount('#app')

  // 在 Pinia 初始化后再更新 i18n 语言
  const languageStore = useLanguageStore()
  languageStore.setLanguage(languageStore.currentLanguage)
})
